package database;

import jade.core.Profile;
import jade.core.ProfileImpl;
import jade.wrapper.AgentContainer;
import jade.wrapper.AgentController;
import jess.Context;
import jess.Userfunction;
import jess.Value;
import jess.ValueVector;

public class DatabaseServer implements Userfunction {

	public String getName() {
		return "DatabaseServer";
	}

	public Value call(ValueVector vv, Context context) {
		try {

			// Get a hold on JADE runtime
			jade.core.Runtime rt = jade.core.Runtime.instance();

			// Exit the JVM when there are no more containers around
			rt.setCloseVM(true);

			// Create a default profile
			Profile p = new ProfileImpl(false);
			// p.setParameter(Profile.MAIN, "false");

			// Create a new non-main container, connecting to the default
			// main container (i.e. on this host, port 1099)
			System.out.println("Launching the agent container ..." + p);
			AgentContainer ac = rt.createAgentContainer(p);

			// Create a new agent, a DummyAgent
			AgentController server = ac.createNewAgent("ServerAgent",
					"database.ServerAgent", new Object[] {});

			// Fire up the agent
			System.out.println("Starting up a Server...");
			server.start();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
